﻿/* 
This file is part of Ingo Karstein's Long Path Support project

**Do not remove this comment**

Please see the project homepage at CodePlex:
  http://longpathsupport.codeplex.com/

Please see my blog:
  http://ikarstein.wordpress.com

This code is used in the project "RoboPowerCopy"
  http://robopowercopy.codeplex.com

Licence: GPLv2
    Ingo Karstein's SharePoint 2010 Custom Ribbon Demo
    Copyright (C) 2011  Ingo Karstein, ikarstein@hotmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

internal static class AttributeHelper
{
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern int SetFileAttributes(string lpFileName, uint dwFileAttributes);

    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern int GetFileAttributes(string lpFileName);

    public static System.IO.FileAttributes? GetAttributes(string fileName)
    {
        int ret = GetFileAttributes(fileName);
        if( ret == -1 )
        {
            return null;
        }

        System.IO.FileAttributes res = 0;
        if( ( ret & (int)LongPathSupport.WIN32_FILEATTRIBUTES.Archive ) > 0 )
            res |= System.IO.FileAttributes.Archive;
        if( ( ret & (int)LongPathSupport.WIN32_FILEATTRIBUTES.Compressed ) > 0 )
            res |= System.IO.FileAttributes.Compressed;
        if( ( ret & (int)LongPathSupport.WIN32_FILEATTRIBUTES.Device ) > 0 )
            res |= System.IO.FileAttributes.Device;

        if( ( ret & (int)LongPathSupport.WIN32_FILEATTRIBUTES.Directory ) > 0 )
            res |= System.IO.FileAttributes.Directory;
        if( ( ret & (int)LongPathSupport.WIN32_FILEATTRIBUTES.Encrypted ) > 0 )
            res |= System.IO.FileAttributes.Encrypted;
        if( ( ret & (int)LongPathSupport.WIN32_FILEATTRIBUTES.Hidden ) > 0 )
            res |= System.IO.FileAttributes.Hidden;
        if( ( ret & (int)LongPathSupport.WIN32_FILEATTRIBUTES.Normal ) > 0 )
            res |= System.IO.FileAttributes.Normal;
        if( ( ret & (int)LongPathSupport.WIN32_FILEATTRIBUTES.NotContentIndexed ) > 0 )
            res |= System.IO.FileAttributes.NotContentIndexed;
        if( ( ret & (int)LongPathSupport.WIN32_FILEATTRIBUTES.Offline ) > 0 )
            res |= System.IO.FileAttributes.Offline;
        if( ( ret & (int)LongPathSupport.WIN32_FILEATTRIBUTES.Readonly ) > 0 )
            res |= System.IO.FileAttributes.ReadOnly;
        if( ( ret & (int)LongPathSupport.WIN32_FILEATTRIBUTES.ReparsePoint ) > 0 )
            res |= System.IO.FileAttributes.ReparsePoint;
        if( ( ret & (int)LongPathSupport.WIN32_FILEATTRIBUTES.SparseFile ) > 0 )
            res |= System.IO.FileAttributes.SparseFile;
        if( ( ret & (int)LongPathSupport.WIN32_FILEATTRIBUTES.System ) > 0 )
            res |= System.IO.FileAttributes.System;
        if( ( ret & (int)LongPathSupport.WIN32_FILEATTRIBUTES.Temporary ) > 0 )
            res |= System.IO.FileAttributes.Temporary;

        return res;
    }

    public static bool SetAttributes(string fileName, System.IO.FileAttributes attributes)
    {
        LongPathSupport.WIN32_FILEATTRIBUTES attr = 0;

        if( ( (int)attributes & (int)System.IO.FileAttributes.Archive ) > 0 )
            attr |= LongPathSupport.WIN32_FILEATTRIBUTES.Archive;
        if( ( (int)attributes & (int)System.IO.FileAttributes.Compressed ) > 0 )
            attr |= LongPathSupport.WIN32_FILEATTRIBUTES.Compressed;
        if( ( (int)attributes & (int)System.IO.FileAttributes.Device ) > 0 )
            attr |= LongPathSupport.WIN32_FILEATTRIBUTES.Device;

        if( ( (int)attributes & (int)System.IO.FileAttributes.Directory ) > 0 )
            attr |= LongPathSupport.WIN32_FILEATTRIBUTES.Directory;
        if( ( (int)attributes & (int)System.IO.FileAttributes.Encrypted ) > 0 )
            attr |= LongPathSupport.WIN32_FILEATTRIBUTES.Encrypted;
        if( ( (int)attributes & (int)System.IO.FileAttributes.Hidden ) > 0 )
            attr |= LongPathSupport.WIN32_FILEATTRIBUTES.Hidden;
        if( ( (int)attributes & (int)System.IO.FileAttributes.Normal ) > 0 )
            attr |= LongPathSupport.WIN32_FILEATTRIBUTES.Normal;
        if( ( (int)attributes & (int)System.IO.FileAttributes.NotContentIndexed ) > 0 )
            attr |= LongPathSupport.WIN32_FILEATTRIBUTES.NotContentIndexed;
        if( ( (int)attributes & (int)System.IO.FileAttributes.Offline ) > 0 )
            attr |= LongPathSupport.WIN32_FILEATTRIBUTES.Offline;
        if( ( (int)attributes & (int)System.IO.FileAttributes.ReadOnly ) > 0 )
            attr |= LongPathSupport.WIN32_FILEATTRIBUTES.Readonly;
        if( ( (int)attributes & (int)System.IO.FileAttributes.ReparsePoint ) > 0 )
            attr |= LongPathSupport.WIN32_FILEATTRIBUTES.ReparsePoint;
        if( ( (int)attributes & (int)System.IO.FileAttributes.SparseFile ) > 0 )
            attr |= LongPathSupport.WIN32_FILEATTRIBUTES.SparseFile;
        if( ( (int)attributes & (int)System.IO.FileAttributes.System ) > 0 )
            attr |= LongPathSupport.WIN32_FILEATTRIBUTES.System;
        if( ( (int)attributes & (int)System.IO.FileAttributes.Temporary ) > 0 )
            attr |= LongPathSupport.WIN32_FILEATTRIBUTES.Temporary;

        int ret = SetFileAttributes(fileName, (uint)attr);
        return ret != 0;
    }

    public static bool CopyAttributes(string sourceFile, string destFile)
    {
        System.IO.FileAttributes? sourceAttr = GetAttributes(sourceFile);
        if( sourceAttr == null )
            return false;
        return SetAttributes(destFile, (System.IO.FileAttributes)sourceAttr);
    }

    public static bool AddAttributes(string fileName, System.IO.FileAttributes attributesToAdd)
    {
        System.IO.FileAttributes? attribs = GetAttributes(fileName);
        if( attribs == null )
            return false;
        attribs = (System.IO.FileAttributes)attribs | attributesToAdd;
        return SetAttributes(fileName, (System.IO.FileAttributes)attribs);
    }

    public static bool RemoveAttributes(string fileName, System.IO.FileAttributes attributesToRemove)
    {
        System.IO.FileAttributes? attribs = GetAttributes(fileName);
        if( attribs == null )
            return false;
        attribs = (System.IO.FileAttributes)attribs & (System.IO.FileAttributes)( (uint)0xFFFFFFFF ^ (uint)attributesToRemove );
        return SetAttributes(fileName, (System.IO.FileAttributes)attribs);
    }

    public static bool RemoveAllAttributes(string fileName)
    {
        return SetAttributes(fileName, System.IO.FileAttributes.Normal);
    }

    public static bool TypeAvailable()
    {
        return true;
    }
}

